﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Financial;
using LuxDiner.Model.Members;
namespace LuxDiner.Command.Financial
{
    public class PaymentCommand
    {
        public static PaymentCollection GetTopPayments(PaymentQuery sea)
        {
            var result = new PaymentCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<view_payment_tops> q = db.DataContext.view_payment_tops;
                if (sea != null)
                {
                    if (sea.CustomerID> 0)
                        q = q.Where(p => p.CustomerID == sea.CustomerID);
                    if (sea.HideZero)
                        q = q.Where(p => p.Balance != 0);
                    if (sea.CustomerType > 0)
                    {
                        var ci = db.DataContext.customers.Where(p => p.CType == sea.CustomerType);
                        q = q.Where(p => ci.Any(o => o.ID == p.CustomerID));
                    }
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<PaymentModel, view_payment_tops, PaymentCollection>(lst);
                    result.TotalRecordsCount = n;

                    if (sea != null)
                    {
                        foreach (var pay in result)
                        {
                            if (sea.GetCustomer)
                            {
                                var customer = db.DataContext.customers.FirstOrDefault(p => p.ID == pay.CustomerID);
                                pay.Customer = Convertor.ConvertTo<CustomerModel, customer>(customer);
                            }
                        }
                    }
                }
            }
            return result;
        }

        public static PaymentCollection GetPayments(PaymentQuery sea)
        {
            var result = new PaymentCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<payment> q = db.DataContext.payments;
                if (sea != null)
                {
                    if (sea.CustomerID > 0)
                        q = q.Where(p => p.CustomerID == sea.CustomerID);
                    if (sea.OperatorUserID > 0)
                        q = q.Where(p => p.OperatorUserID == sea.OperatorUserID);
                    if (sea.NoteDateBefore != null)
                        q = q.Where(p => p.NoteDate.CompareTo(sea.NoteDateBefore.Value) >= 0);
                    if (sea.NoteDateAfter!=null)
                        q = q.Where(p => p.NoteDate.CompareTo(sea.NoteDateAfter.Value) <= 0);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<PaymentModel, payment, PaymentCollection>(lst);
                    result.TotalRecordsCount = n;
                    if (sea != null)
                    {
                        foreach (var m in result)
                        {
                            if (sea.GetCustomer)
                            {
                                var d = lst.Find(p => p.ID == m.ID);
                                m.Customer = Convertor.ConvertTo<CustomerModel, customer>(d.customer);
                            }
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 取所有余额总和（供应商和顾客)
        /// </summary>
        /// <returns></returns>
        public static decimal[] GetTotalBalance()
        {
            var result = new decimal[2];
            using (DBCentre db = new DBCentre())
            {
                IQueryable<view_payment_tops> q = db.DataContext.view_payment_tops;
                var cq = db.DataContext.customers.Where(p => p.CType == 1).Select(p => p.ID);
                var sq = db.DataContext.customers.Where(p => p.CType == 2).Select(p => p.ID);
                result[0] = q.Where(p => cq.Contains(p.CustomerID)).Sum(p => p.Balance);
                result[1] = q.Where(p => sq.Contains(p.CustomerID)).Sum(p => p.Balance);
            }
            return result;
        }
    }
}
